package newx.com.control.progress
{
	import flash.display.DisplayObject;
	import flash.display.MovieClip;
	
	import newx.com.NxUIComponent;
	import newx.com.ResManager;
	import newx.com.control.IProgressBar;
	
	public class NxProgressBar extends NxUIComponent implements IProgressBar
	{
		private var _skin				:String			=	"";
		
		private var _total				:Number			=	100;
		
		private var _current			:Number			=	0;
		
		private var _percent			:Number			=	0;
		
		private var _contentEx			:DisplayObject	=	null;
		
		private var _trueWidth			:Number			=	0;
		
		private var _trueHeight		:Number			=	0;

		/**
		 * 皮肤显示对象
		 * */
		private var _skinView			:MovieClip		=	null;
		/**初始皮肤坐标===========================================================*/
		private var _skin_line_left	:Number			=	0;
		
		private var _skin_line_right	:Number			=	0;
		
		private var _skin_line_top		:Number			=	0;
		
		private var _skin_line_buttom	:Number			=	0;
		//====================================================================
		
		public function NxProgressBar()
		{
			super();
		}
		
		override public function set width(value:Number):void
		{
			if(value != _trueWidth)
			{
				_trueWidth	=	value;
				updateSkinWidth();
				updataPercent();
			}
		}
		
		override public function set height(value:Number):void
		{
			if(value != _trueHeight)
			{				
				_trueHeight	=	value;				
				updataSkinHeight();
				updataPercent();
			}
		}
		
		public function get skin():String
		{
			return _skin;
		}
		
		public function set skin(val:String):void
		{
			if(_skin != val)
			{
				_skin	=	val;
				updateSkin();
				updataPercent();
			}
		}
		
		public function get total():Number
		{
			return _total;
		}
		
		public function set total(val:Number):void
		{
			if(_total != val)
			{
				_total	=	val;
				if(_total == 0)
				{
					percent	=	0;
				}
				else
				{
					percent	=	_current/_total;
				}				
			}
		}
		
		public function get current():Number
		{
			return _current;
		}
		
		public function set current(val:Number):void
		{
			if(_current != val)
			{
				_current	=	val;
				
				if(_total == 0)
				{
					percent	=	0;
				}
				else
				{
					percent	=	_current/_total;
				}				
			}
		}
		
		public function get percent():Number
		{
			return _percent;
		}
		
		public function set percent(val:Number):void
		{
			if(_percent != val)
			{
				_percent	=	val;
				updataPercent();
			}			
		}
		
		public function get contentEx():DisplayObject
		{
			return _contentEx;
		}
		
		public function set contentEx(val:DisplayObject):void
		{
			if(val != _contentEx)
			{
				if(val == null)
				{
					if(_contentEx.parent == this)
					{
						removeChild(_contentEx);
						_contentEx	=	null;
					}
				}
				else
				{
					if(val.parent == null)
					{
						_contentEx	=	val;
						addChild(_contentEx);
					}
				}
			}
		}
		
		/**
		 * 更新皮肤
		 * */
		private function updateSkin():void
		{
			var tView:MovieClip;
			
			try
			{
				tView	=	ResManager.newResClass(_skin) as MovieClip;
			}
			catch(e:Error)
			{
				tView	=	null;
			}
			
			if(tView == null)
			{
				return;
			}
			
			if(_skinView != null)
			{
				
				_skinView.parent.removeChild(_skinView);
			}
						
			_skinView	=	tView;
			
			_skin_line_left		=	_skinView.loadingLine.x
			_skin_line_right	=	_skinView.width	-	
				_skinView.loadingLine.x	-	_skinView.loadingLine.width;
			_skin_line_top		=	_skinView.loadingLine.y
			_skin_line_buttom	=	_skinView.height	-	
				_skinView.loadingLine.y	-	_skinView.loadingLine.height;
			
			addChild(_skinView);
			
			if(_trueWidth > 0)
			{
				updateSkinWidth()
			}
			else
			{
				_trueWidth	=	_skinView.width;
			}
			
			if(_trueHeight > 0)
			{
				updataSkinHeight()
			}
			else
			{
				_trueHeight	=	_skinView.height;
			}
			
			updataPercent();
		}
		
		private function updateSkinWidth():void
		{
			if(_skinView != null)
			{					
				_skinView.loadingLine.x			=	_skin_line_left;							
				_skinView.loadingLine.width		=	_trueWidth	-	
					_skin_line_left	-	_skin_line_right;
				_skinView.loadingLineBack.width	=	_trueWidth;
			}
			
			if(_contentEx != null)
			{
//				_contentEx.x	*=	_sx;
			}
		}
		
		private function updataSkinHeight():void
		{
			if(_skinView != null)
			{
				_skinView.loadingLine.y				=	_skin_line_top;							
				_skinView.loadingLine.height		=	_trueHeight	-	
					_skin_line_top	-	_skin_line_buttom;
				_skinView.loadingLineBack.height	=	_trueHeight;
			}
			
			if(_contentEx != null)
			{
//				_contentEx.y	*=	_sy;
			}
		}
		
		/**
		 * 更新进度在界面上的表现
		 * */
		private function updataPercent():void
		{
			if(_skinView != null)
			{
				_skinView.loadingLine.width	= (_trueWidth	-	
					_skin_line_left	-	_skin_line_right)*_percent;
			}
		}
	}
}